#!/usr/bin/env perl

use IPC::Run 'run';
sub out;
sub err;

my %CL = qw(
	darkred     0;31
	red         1;31
	
	green       0;32
	lime        1;32
	
	brown       0;33
	yellow      1;33

	navy        0;34
	blue        1;34
	
	magenta     0;35
	pink        1;35
	
	darktail    0;36
	tail        1;36
	
	gray        0;37
	white       1;37
	
	clear       0;0
);
map { $_="\033[${_}m" } values %CL;
my $CL = $CL{clear};

my %ST = (
	'?' => 'gray',
	'M' => 'blue',
	'C' => 'red',
	'U' => 'tail',
	'P' => 'tail',
	'A' => 'magenta',
);

#my $CONS = ! 
-f STDOUT and map { $_='' } values %CL;

my $cmd;
if ( -d '.svn' ) {
	$cmd = "svn add @ARGV";
}
elsif( -d 'CVS' ) {
	$cmd = "cvs add @ARGV";
}
else {
	die "Neither CVS nor SVN reporitory\n";
}
print STDERR "\$ $cmd\n";
run [split /\s+/,$cmd], \(undef),\&out,\&err;

sub csay {
	my $color = shift;
	my $msg = shift;
	exists $CL{ $color } or die "Bad color specification: $color\n";
	print $CL{ $color } . sprintf($msg,@_) . $CL . "\n";
}

sub out {
	for (map split(/\n/),@_) {
		chomp;
		my ($st,$file) = split /\s+/,$_,2;
		if ( -f $file ) {}
		elsif ( -l $file ) {
			my $post = '';
			if ( -d $file ) { $post = '/' }
			$file .= $post.' ('.readlink($file).$post.')'
		}
		elsif ( -d $file ) { $file .= '/' }
		exists $ST{$st} or warn "$st nave no defined color\n";
		csay( ( $ST{$st} || 'green' ), "%s\t%s",$st,$file );
		#printf "$st => $file\n"
	}
}

sub err {
	for (map split(/\n/),@_) {
		next if /^cvs server: New directory .* ignored/;
		chomp;
		csay( red => $_ );
	}
}

